<?php
namespace app\service;

class ZtPay
{
    private $appid = '';
    private $key = '';
    private $url = 'https://sapi.ztpay.org/api/v2';

    function __construct($appid, $key)
    {
        $this->appid = $appid;
        $this->key = $key;
    }

    /**
     * 查询余额
     * @param $name 查询币种
     * @param $address 查询地址
     * @return bool
     */
    public function get_balance($name,$address)
    {
        $data = array(
            'appid' => $this->appid,
            'method' => __FUNCTION__,
            'name' => $name,
            'address' => $address,
        );
        $sign = getSign($data,$this->key);
        $data['sign'] = $sign;
        $res = $this->request_post($this->url,$data);
        if($res && $res['code'] == 0){
            return $res['data'];
        }
        return false;
    }

    /**
     * 创建地址
     * @param $name 币种名
     * @return bool
     */
    public function get_address($name)
    {
        $data = array(
            'appid' => $this->appid,
            'method' => __FUNCTION__,
            'name' => $name
        );
        $sign = getSign($data,$this->key);
        $data['sign'] = $sign;
        $res = $this->request_post($this->url,$data);
        if($res && $res['code'] == 0){
            return $res['data'];
        }
        return false;
    }

    /**
     * 发起交易
     * @param $name 币种名
     * @param $from 转出地址
     * @param $to 转入地址
     * @param $amount 金额
     * @param string $type 类型
     * @return bool|mixed
     */
    public function transfer($name,$from,$to,$amount,$type='ETH')
    {
        $data = array(
            'appid' => $this->appid,
            'method' => __FUNCTION__,
            'name' => $name,
            'from' => $from,
            'to' => $to,
            'amount' => $amount,
        );
        if($type == 'ETH'){
            $price_data = $this->get_eth_gasprice();
            $data['gas'] = 70000;
            $data['gasPrice'] = $price_data['fastest'];
            //$data['nonce'] = 0;
        }else{
            //手续费
            //$data['fee_amount'] = '0.00004';
        }
        $sign = getSign($data,$this->key);
        $data['sign'] = $sign;
        $res = $this->request_post($this->url,$data);
        return $res;
    }

    //查询gas价格
    /*public function get_eth_gasprice()
    {
        $data = array(
            'appid' => $this->appid,
            'method' => __FUNCTION__,
        );
        $sign = getSign($data,$this->key);
        $data['sign'] = $sign;
        $res = $this->request_post($this->url,$data);
        if($res && $res['code'] == 0){
            return $res['data'];
        }
        return false;
    }*/

    public function get_eth_gasprice()
    {
        $url = "https://www.etherchain.org/api/gasPriceOracle";
        $res = curlRequest($url);
        if($res){
            $res = json_decode($res,true);
        }
        return $res;
    }

    //查询行情价格
    public function market()
    {
        $data = array(
            'appid' => $this->appid,
            'method' => __FUNCTION__,
        );
        $sign = getSign($data,$this->key);
        $data['sign'] = $sign;
        $res = $this->request_post($this->url,$data);
        if($res && $res['code'] == 0){
            return $res['data'];
        }
        return false;
    }

    /**
     * 校验地址
     * @param $name 币名
     * @param $address 地址
     * @return bool|mixed
     */
    public function check_address($name,$address)
    {
        $data = array(
            'appid' => $this->appid,
            'method' => __FUNCTION__,
            'name' => $name,
            'address' => $address,
        );
        $sign = getSign($data,$this->key);
        $data['sign'] = $sign;
        $res = $this->request_post($this->url,$data);
        //dump($res);
        if($res && $res['code'] == 0){
            return true;
        }
        return $res['message'];
    }

    public function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSLVERSION, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        $data = curl_exec($ch);
        curl_close($ch);

        return json_decode($data,true);
    }
}